﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class RoleMenuProcessBll 
    {
        public static RoleMenuCls[] ReadingRoleMenus(string SiteId, string RoleId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableRoleMenu where frkRoleId=@RoleId",
                             new DbParam[] 
                    { 
                        new DbParam("RoleId", RoleId),
                    });


            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new RoleMenuCls[0];
            }
            RoleMenuCls[] CmsRoleMenus = RoleMenuParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsRoleMenus;
        }

        public static RoleMenuCls CreateRoleMenuModel(string SiteId, string RoleMenuId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableRoleMenu " +
                                               "     where RoleMenuId=@RoleMenuId",
                    new DbParam[] 
                    { 
                        new DbParam("RoleMenuId", RoleMenuId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            RoleMenuCls ORoleMenu = RoleMenuParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ORoleMenu;
        }

        public static void SaveRoleMenu(
            string SiteId,
            string AssetCode,
            string RoleId,
            RoleMenuCls[] RoleMenus)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string DelQuery =
                " Delete from TableRoleMenu where frkRoleId=@RoleId";
            DBService.ExecuteNonQuery(null, DelQuery,
                  new DbParam[] 
                    { 
                        new DbParam("RoleId", RoleId),
                    });

            for (int iIndex = 0; iIndex < RoleMenus.Length; iIndex++)
            {
                DBService.Insert(null, "TableRoleMenu",
                        new DbParam[] 
                    { 
                        new DbParam("RoleMenuId", System.Guid.NewGuid().ToString()),
                        new DbParam("frkRoleId",RoleId),
                        new DbParam("WebFolderId",RoleMenus[iIndex].WebFolderId),
                        new DbParam("Allow",RoleMenus[iIndex].Allow),
                        new DbParam("AssetCode",RoleMenus[iIndex].AssetCode),
                    });
            }
        }

        public static void DeleteRoleMenu(string SiteId, string RoleMenuId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableRoleMenu where RoleMenuId=@RoleMenuId ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("RoleMenuId", RoleMenuId),
                    });
        }
    }
}

